package com.zahb.qadx.ui.view.captcha

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import com.luozm.captcha.DefaultCaptchaStrategy

/**
 * Created by Zdw on 2021/06/08 9:57
 */

class CustomCaptchaStrategy(ctx: Context) : DefaultCaptchaStrategy(ctx) {

    override fun getBlockShape(blockSize: Int): Path {
        val gap = blockSize / 3.5f
        val path = Path()
        path.moveTo(0f, gap * 0.5f)
        path.rLineTo(0f, gap)
        path.arcTo(
                -gap * 0.5f, gap * 1.5f, gap * 0.5f, gap * 2.5f,
                -90f, 180f, false
        )
        path.rLineTo(0f, gap)
        path.rLineTo(gap, 0f)
        path.arcTo(
                gap, gap * 3f, gap * 2f, gap * 4f,
                -180f, 180f, false
        )
        path.rLineTo(gap, 0f)
        path.rLineTo(0f, -gap)
        path.arcTo(
                gap * 2.5f, gap * 1.5f, gap * 3.5f, gap * 2.5f,
                90f, -180f, false
        )
        path.rLineTo(0f, -gap)
        path.rLineTo(-gap, 0f)
        path.arcTo(
                gap, 0f, gap * 2f, gap,
                0f, -180f, false
        )
        path.rLineTo(-gap, 0f)
        path.close()
        return path
    }

    override fun decoreateSwipeBlockBitmap(canvas: Canvas?, shape: Path?) {
        if (shape == null) return
        canvas?.let {
            val paint = Paint(Paint.ANTI_ALIAS_FLAG)
            paint.color = Color.WHITE
            paint.style = Paint.Style.STROKE
            paint.strokeWidth = 5f
            it.drawPath(shape, paint)
        }
    }
}